package com.aimuceliang.aimuceliang.ui.activity;

import android.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;

import com.aimuceliang.aimuceliang.MyApp;
import com.aimuceliang.aimuceliang.bean.HttpResult;
import com.aimuceliang.aimuceliang.dev.R;
import com.aimuceliang.aimuceliang.net.HttpSubscriber;
import com.aimuceliang.aimuceliang.ui.BaseActivity;
import com.aimuceliang.aimuceliang.utils.SharePreferenceUtil;
import com.aimuceliang.aimuceliang.utils.StringUtil;
import com.aimuceliang.aimuceliang.utils.T;
import com.aimuceliang.aimuceliang.utils.XLog;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscription;

/**
 * Created by ziabo on 2017/7/27.
 * 登录界面
 */

public class LoginActivity extends BaseActivity {
    private AlertDialog dialog;

    @BindView(R.id.main_iv_left)
    ImageView vMainIvLeft;
    @BindView(R.id.et_username)
    EditText vEtUsername;
    @BindView(R.id.et_pwd)
    EditText vEtPwd;
    @BindView(R.id.cb_remember)
    CheckBox vCbRemember;

    private String username;
    private String password;

    @Override
    protected int initContentView() {
        return R.layout.activity_login;
    }

    @Override
    protected void initUIAndListener() {
        setTitle("账号登录");
        vMainIvLeft.setVisibility(View.GONE);//左边箭头隐藏
        boolean isRemember = SharePreferenceUtil.getIsRememberPwd(this);
        String spUser = SharePreferenceUtil.getUsername(this);
        String spPwd = SharePreferenceUtil.getPassword(this);
        if (isRemember) {
            vCbRemember.setChecked(true);
            vEtUsername.setText(spUser);
            vEtPwd.setText(spPwd);
        } else {
            vCbRemember.setChecked(false);
            vEtUsername.setText(spUser);
        }
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.bt_login, R.id.tv_apply_account})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_login:

                //startActivity(MainActivity.class);
                //finish();

                username = vEtUsername.getText().toString().trim();
                password = vEtPwd.getText().toString().trim();
                if (!StringUtil.checkStr(username)) {
                    T.showShort("密码不能为空");
                    return;
                }
                if (!StringUtil.checkStr(password)) {
                    T.showShort("密码不能为空");
                    return;
                }

                Subscription s = MyApp.getApiService().login(new HttpSubscriber<HttpResult>() {
                    @Override
                    public void onFinished() {

                    }

                    @Override
                    public void onNext(HttpResult httpResult) {

                        XLog.d("TTTT","httpResult.code::" + httpResult.code);

                        if (httpResult.code.equals("0")) {
                            if (vCbRemember.isChecked()) {
                                SharePreferenceUtil.saveUsername(LoginActivity.this, username);
                                SharePreferenceUtil.savePassword(LoginActivity.this, password);
                                SharePreferenceUtil.saveIsRememberPwd(LoginActivity.this, true);
                            }
                            startActivity(MainActivity.class);
                            finish();
                        } else {
                            initDialog();
                            //T.showShort(httpResult.message);
                        }
                    }
                }, username, password);
                addSubscription(s);
                break;
            case R.id.tv_apply_account:
                startActivity(ApplyAccountActivity.class);
                break;
        }

    }

    private void initDialog() {
        XLog.d("密码错误");
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View view = LayoutInflater.from(this).inflate(R.layout.layout_login_error, null);
        view.findViewById(R.id.login_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });
        builder.setView(view);
        builder.setCancelable(true);
        dialog = builder.create();
        dialog.show();
    }
}
